package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hermit.qmst.R;
import com.hermit.qmst.UI.XlistView.XListView;
import com.hermit.qmst.adapter.BottleAdapter;
import com.hermit.qmst.bean.BottleBean;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.GsonUtil;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * Created by Administrator on 2016/11/30 0030.
 */
@ContentView(R.layout.new_activity_bottle)
public class BottleActivity extends Activity implements XListView.IXListViewListener, View.OnClickListener {
    @ViewInject(R.id.iv_back)
    ImageView mIvTopLeft;
    @ViewInject(R.id.xListView)
    XListView mListView;
    @ViewInject(R.id.tv_title)
    TextView mIvTitle;
    private int mPageIndex = 0;
    private int mPageCount = 20;
    private BottleAdapter adapter;
    private String mUrl;
    private String TAG = "BottleActivity";
    public static String type = "drift";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvTopLeft.setVisibility(View.VISIBLE);
        mIvTopLeft.setOnClickListener(this);
        mIvTitle.setText("官方圈");
        adapter = new BottleAdapter(BottleActivity.this);
        mListView.setXListViewListener(this);
        mListView.setAdapter(adapter);
        mListView.setPullLoadEnable(false);
//        sensorEventListener = new JCVideoPlayer.JCAutoFullscreenListener();


        getData();
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        Calendar c = Calendar.getInstance();
        mListView.setRefreshTime(c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND));
    }

    private void getData() {
        onLoad();
        mUrl = Common.mUrl + "szdh/messages.php?uid=" + Common.mUserId + "&agentId=" + Common.mAgentId + "&agent_id_third=" + Common.mAgentThirdId + "&type="+type+"&page=" + mPageIndex + "&size=" + mPageCount;
        RequestHttp.getHttp(BottleActivity.this, mUrl, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                try {
                    if (jsonObject.getBoolean("state")) {
                        Type dataType = new TypeToken<List<BottleBean>>() {
                        }.getType();
                        ArrayList<BottleBean> bean = GsonUtil.getGson().fromJson(jsonObject.getString("data"), dataType);
                        if (bean.size() > 0) {
                            if (bean.size() >= mPageCount) {
                                mListView.setPullLoadEnable(true);
                            }
                            adapter.addData(bean);
                        } else {
                            adapter.data.clear();
                        }
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } finally {
                    onLoad();
                }
            }
        });
    }

    @Override
    public void onLoadMore() {
        mPageIndex++;
        getData();
    }

    @Override
    public void onRefresh() {
        mPageIndex = 0;
        adapter = new BottleAdapter(BottleActivity.this);
        mListView.setAdapter(adapter);
        adapter.data.clear();
        getData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
